package flc.ast.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.EditRecordActivity;
import flc.ast.activity.SelPicActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.adapter.RecordAdapter;
import flc.ast.bean.RecordBean;
import flc.ast.databinding.FragmentFormatBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.UriUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FormatFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FormatFragment extends BaseNoModelFragment<FragmentFormatBinding> {

    private RecordAdapter recordAdapter;
    private List<RecordBean> listShow = new ArrayList<>();
    private Dialog myEditDialog, myRenameDialog, myDeleteDialog;
    private EditText dialogRename;
    private int selPosition;

    @Override
    protected int onCreate() {
        return R.layout.fragment_format;
    }

    @Override
    protected void initView() {
        mDataBinding.tvEnterEdit.setOnClickListener(this);
        mDataBinding.ivVideoFormat.setOnClickListener(this);
        mDataBinding.ivPicFormat.setOnClickListener(this);
        mDataBinding.tvMoreRecord.setOnClickListener(this);
        mDataBinding.rvRecordList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recordAdapter = new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.addChildClickViewIds(R.id.ivRecordItemEdit);
        recordAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvEnterEdit:
                SelVideoActivity.type = 6;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.ivVideoFormat:
                SelVideoActivity.type = 7;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.ivPicFormat:
                startActivity(SelPicActivity.class);
                break;
            case R.id.tvMoreRecord:
                startActivity(EditRecordActivity.class);
                break;
            case R.id.tvDialogEditRename://重命名
                renameDialog();
                myEditDialog.dismiss();
                myRenameDialog.show();
                dialogRename.setText(listShow.get(selPosition).getName());
                break;
            case R.id.tvDialogRenameConfirm:
                listShow.get(selPosition).setName(dialogRename.getText().toString().trim());
                SPUtil.putObject(mContext, listShow, new TypeToken<List<RecordBean>>() {
                }.getType());
                getData();
                myRenameDialog.dismiss();
                break;
            case R.id.tvDialogRenameCancel:
                myRenameDialog.dismiss();
                break;
            case R.id.tvDialogEditShare://分享
                share(listShow.get(selPosition).getPath());
                break;
            case R.id.tvDialogEditDelete://删除
                deleteDialog();
                myEditDialog.dismiss();
                myDeleteDialog.show();
                break;
            case R.id.tvDialogDeleteConfirm:
                listShow.remove(selPosition);
                SPUtil.putObject(mContext, listShow, new TypeToken<List<RecordBean>>() {
                }.getType());
                getData();
                myDeleteDialog.dismiss();
                break;
            case R.id.tvDialogDeleteCancel:
                myDeleteDialog.dismiss();
                break;
            case R.id.tvDialogEditCancel://取消
                myEditDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void share(String path) {
        Uri uri = UriUtil.path2Uri(mContext, path);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("video/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(intent, "Share"));
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()){
            case R.id.ivRecordItemEdit:
                moreDialog();
                myEditDialog.show();
                selPosition=position;
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData(){
        listShow.clear();
        List<RecordBean> listSp = SPUtil.getObject(mContext, new TypeToken<List<RecordBean>>() {
        }.getType());
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            if(listSp.size()>3){
                listShow=listSp.subList(0, 3);
            }
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.llPlayRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.llPlayRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    //编辑弹框
    private void moreDialog() {
        myEditDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_edit, null);
        myEditDialog.setContentView(inflate);
        myEditDialog.setCancelable(true);
        Window dialogWindow = myEditDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView rename = inflate.findViewById(R.id.tvDialogEditRename);
        TextView share = inflate.findViewById(R.id.tvDialogEditShare);
        TextView delete = inflate.findViewById(R.id.tvDialogEditDelete);
        TextView cancel = inflate.findViewById(R.id.tvDialogEditCancel);
        rename.setOnClickListener(this);
        share.setOnClickListener(this);
        delete.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    //重命名弹框
    private void renameDialog() {
        myRenameDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_rename, null);
        myRenameDialog.setContentView(inflate);
        myRenameDialog.setCancelable(true);
        Window dialogWindow = myRenameDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView right = inflate.findViewById(R.id.tvDialogRenameConfirm);
        TextView cancel = inflate.findViewById(R.id.tvDialogRenameCancel);
        dialogRename = inflate.findViewById(R.id.etDialogRenameText);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView cancel = inflate.findViewById(R.id.tvDialogDeleteCancel);
        TextView right = inflate.findViewById(R.id.tvDialogDeleteConfirm);
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}